package com.huawei.esdk.uc.device.u19.attendee.convert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huawei.ecs.bin.service.AddAtte;
import com.huawei.ecs.bin.service.AddAtteAck;
import com.huawei.ecs.bin.service.DelAtte;
import com.huawei.ecs.bin.service.DelAtte.DelAtteInfo;
import com.huawei.ecs.bin.service.DelAtteAck;
import com.huawei.ecs.bin.service.OperateAtte;
import com.huawei.ecs.bin.service.SetAtte;
import com.huawei.ecs.bin.service.SetChairman;
import com.huawei.ecs.bin.service.data.Atte.AtteAttribute;
import com.huawei.ecs.bin.service.data.AtteInfo;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.uc.domain.model.bean.AttendeeInfo;
import com.huawei.esdk.uc.domain.model.u19.Attendee;

public class AttendeeCapabilityConvert
{
    public OperateAtte getOperateAttendeeModel2Bin(Attendee attendee)
    {
        OperateAtte atte = new OperateAtte();
        
        // 转换操作类型
        if ("0".equals(attendee.getOperate()))
        {
            // 踢出与会人
            atte.operate_ = OperateAtte.Operate.REMOVE;
        }
        else if ("1".equals(attendee.getOperate()))
        {
            // 邀请与会人
            atte.operate_ = OperateAtte.Operate.ADD;
        }
        else
        {
            // 未知
            atte.operate_ = OperateAtte.Operate.UNKNOWN;
        }
        
        // 转换与会人信息
        AtteInfo atteInfo = new AtteInfo();
        
        // 0：未注册，1：已注册
        atteInfo.isRegister_ = "0".equals(attendee.getIsRegister()) ? false : true;
        atteInfo.attNumber_ = attendee.getAttendeeNumber();
        atteInfo.confNo_ = Integer.parseInt(attendee.getConfRoom());
        atteInfo.attType_ = getAtteAttributeMapping(attendee.getAttribute());
        
        atte.atteInfo_ = atteInfo;
        return atte;
    }
    
    public SetChairman getTransferChairman(Attendee attendee)
    {
        SetChairman chairman = new SetChairman();
        chairman.confNo_ = Integer.parseInt(attendee.getConfRoom());
        chairman.newDn_ = attendee.getNewChairNumber();
        chairman.oldDn_ = attendee.getOldChairNumber();
        return chairman;
    }
    
    private AtteAttribute getAtteAttributeMapping(String attr)
    {
        Map<String, AtteAttribute> map = new HashMap<String, AtteAttribute>();
        map.put("0", AtteAttribute.NOMAL);
        map.put("1", AtteAttribute.FORBIDEN);
        map.put("2", AtteAttribute.WITHOUT_RSP);
        map.put("3", AtteAttribute.CHAIRMAN);
        map.put("4", AtteAttribute.UNKNOWN);
        
        return map.get(attr);
    }
    
    public AddAtte getAddAttendeeU19Model2Bin(Attendee attendee)
    {
        AddAtte addAtte = new AddAtte();
        
        // 转换与会人信息
        AtteInfo atteInfo = new AtteInfo();
        
        atteInfo.attNumber_ = attendee.getAttendeeNumber();
        atteInfo.confNo_ = Integer.parseInt(attendee.getConfRoom());
        atteInfo.attType_ = getAtteAttributeMapping(attendee.getAttribute());
        
        addAtte.atteInfo_ = atteInfo;
        
        return addAtte;
    }
    
    public SDKResult<AttendeeInfo> getAddAttendeeU19Bin2Model(AddAtteAck response)
    {
        SDKResult<AttendeeInfo> sdkResult = new SDKResult<AttendeeInfo>();
        
        AttendeeInfo attendeeInfo = new AttendeeInfo();
        
        if (null != response.errAttNumbers_ && !response.errAttNumbers_.isEmpty())
        {
            attendeeInfo.setAttendeeNumbers(new ArrayList<String>());
            
            for (String str : response.errAttNumbers_)
            {
                attendeeInfo.getAttendeeNumbers().add(str);
            }
            
            attendeeInfo.setMsgContentLen(String.valueOf(response.errAttNumbers_.size()));
        }
        
        sdkResult.setErrCode(response.retCode_);
        
        attendeeInfo.setRecordNum(String.valueOf(response.retCode_ == 0 ? 0 : 1));
        
        sdkResult.setDescription(response.getCmdCode().name() + " " + response.getCmdCode().value());
        sdkResult.setResult(attendeeInfo);
        
        return sdkResult;
    }
    
    public DelAtte getDelAttendeeU19Model2Bin(Attendee attendee)
    {
        DelAtte delAtte = new DelAtte();
        delAtte.confNo_ = Integer.parseInt(attendee.getConfRoom());
        
        List<com.huawei.esdk.uc.domain.model.bean.DelAtteInfo> delAtteInfoModels = attendee.getDelAtteInfos();
        List<DelAtteInfo> attends_ = new ArrayList<DelAtteInfo>();
        DelAtteInfo delAtteInfo = null;
        
        if (null != delAtteInfoModels && !delAtteInfoModels.isEmpty())
        {
            for (com.huawei.esdk.uc.domain.model.bean.DelAtteInfo delAtteInfoModel : delAtteInfoModels)
            {
                delAtteInfo = new DelAtteInfo();
                // 0：未注册，1：已注册
                delAtteInfo.isRegister_ = "0".equals(delAtteInfoModel.getIsRegister()) ? false : true;
                delAtteInfo.attNumber_ = delAtteInfoModel.getAttendeeNumber();
                attends_.add(delAtteInfo);
            }
        }
        
        delAtte.attends_ = attends_;
        
        return delAtte;
    }
    
    public SDKResult<AttendeeInfo> getDelAttendeeU19Bin2Model(DelAtteAck response)
    {
        SDKResult<AttendeeInfo> sdkResult = new SDKResult<AttendeeInfo>();
        
        AttendeeInfo attendeeInfo = new AttendeeInfo();
        
        if (null != response.errAttNumbers_ && !response.errAttNumbers_.isEmpty())
        {
            attendeeInfo.setAttendeeNumbers(new ArrayList<String>());
            
            for (String str : response.errAttNumbers_)
            {
                attendeeInfo.getAttendeeNumbers().add(str);
            }
            
            attendeeInfo.setMsgContentLen(String.valueOf(response.errAttNumbers_.size()));
        }
        
        sdkResult.setErrCode(response.retCode_);
        
        attendeeInfo.setRecordNum(String.valueOf(response.retCode_ == 0 ? 0 : 1));
        
        sdkResult.setDescription(response.getCmdCode().name() + " " + response.getCmdCode().value());
        sdkResult.setResult(attendeeInfo);
        
        return sdkResult;
    }
    
    public SetAtte getModifyAttendeeAuthority(Attendee attendee)
    {
        SetAtte atte = new SetAtte();
        
        // 转换与会人信息
        AtteInfo atteInfo = new AtteInfo();
        
        // 0：未注册，1：已注册
        atteInfo.isRegister_ = "0".equals(attendee.getIsRegister()) ? false : true;
        atteInfo.attNumber_ = attendee.getAttendeeNumber();
        atteInfo.confNo_ = Integer.parseInt(attendee.getConfRoom());
        atteInfo.attType_ = getAtteAttributeMapping(attendee.getAttribute());
        
        atte.atteInfo_ = atteInfo;
        return atte;
    }
}
